/*
	普通聊天
*/

package net.sf.odinms.net.channel.handler;


import net.sf.odinms.client.MapleClient;
import net.sf.odinms.client.messages.CommandProcessor;
import net.sf.odinms.net.AbstractMaplePacketHandler;
import net.sf.odinms.tools.MaplePacketCreator;
import net.sf.odinms.tools.data.input.SeekableLittleEndianAccessor;

public class GeneralchatHandler extends AbstractMaplePacketHandler {

    @Override
    public void handlePacket(SeekableLittleEndianAccessor slea, MapleClient c) {
 // [B3 00] [E5 B3 29 00] [00] 08 00 CE D2 CA C7 C4 E3 C2 E8 00
        slea.readInt();
        String text = slea.readMapleAsciiString();
        int show = slea.readByte();

        if (c.getPlayer().getCheatTracker().textSpam(text) && !c.getPlayer().isGM()) {
            c.getSession().write(MaplePacketCreator.serverNotice(1, "您发送的字数已经达到上限!"));
            return;
        }
        if (text.length() > 70 && !c.getPlayer().isGM()) {
            return;
        }
        if (!CommandProcessor.getInstance().processCommand(c, text)) {
            if (c.getPlayer().isMuted() || (c.getPlayer().getMap().getMuted() && !c.getPlayer().isGM())) {
                c.getPlayer().dropMessage(5, c.getPlayer().isMuted() ? "You are " : "The map is " + "muted, therefore you are unable to talk.");
                return;
            }

            if(c.getPlayer().isGM()){
                c.getPlayer().getMap().broadcastMessage(MaplePacketCreator.getChatText(c.getPlayer().getId(), text, c.getChannelServer().allowGmWhiteText(), show));
            } else {  
                c.getPlayer().getMap().broadcastMessage(MaplePacketCreator.getChatText(c.getPlayer().getId(), text.toLowerCase().replaceAll("map", " map"), false, show));                
            }
        }
    }
}